package com.openharmony.filepicker.engine;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.openharmony.filepicker.ResourceTable;
import ohos.agp.components.Image;
import ohos.agp.components.element.Element;
import ohos.app.Context;
import ohos.utils.net.Uri;
import org.jetbrains.annotations.Nullable;

public class GlideEngine implements ImageEngine{

    @Override
    public void loadImage(Context context, Image image, String url, int placeholder) {

        if (context == null || image == null) {
            return;
        }

        Glide.with(context)
                .load(Uri.parse(url))
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .skipMemoryCache(true)
                .addListener(new RequestListener<Element>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object o, Target<Element> target, boolean b) {
                        image.setPixelMap(ResourceTable.Media_ic_unknown_file_picker);
                        return true;
                    }

                    @Override
                    public boolean onResourceReady(Element element,
                                                   Object o,
                                                   Target<Element> target,
                                                   DataSource dataSource,
                                                   boolean b) {
                        return false;
                    }
                })
                .into(image);
    }
}
